package
{
	import flash.events.Event;
	
	import mx.controls.CheckBox;
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.events.FlexEvent;

	public class TagCheckBoxItemRenderer extends TreeItemRenderer
	{	
		private var checkBox: CheckBox;
		
		public function TagCheckBoxItemRenderer() {
			super();
			addEventListener("dataChange", function(event: FlexEvent): void {
				if (data)
					checkBox.selected = (data as CheckableTagTreeInfo).checked;
			});
		}
		
		public function get checked(): Boolean {
			return checkBox.selected;
		}
		
		public function set checked(value: Boolean): void {
			checkBox.selected = value;
		}
		
		override protected function createChildren(): void {
			super.createChildren();			
			checkBox = new CheckBox();
			addChild(checkBox);
			checkBox.selected = false; // to set it dirty
			checkBox.data = this;
			checkBox.selected = false;
			checkBox.addEventListener(Event.CHANGE, checkBoxChange, false, 0, true);
		}
		
		private function checkBoxChange(event: Event): void {
			owner.dispatchEvent(new TagCheckBoxChangedEvent(this));
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number): void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);					
			if (icon) {
 				checkBox.height = icon.height;
			}				
		}
	}
}